﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entidades.Eventos;
using Negocio.Eventos;

public partial class Eventos_GestionarReserva : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        btnBuscar.ServerClick += new EventHandler(btnBuscar_ServerClick);
        btnNuevo.ServerClick += new EventHandler(btnNuevo_ServerClick);

        if (!Page.IsPostBack)
        {

            List<EveTipoEvento> eveTipoEvento = EveTipoEventoBLL.listar();
            List<ListItem> listListItem = eveTipoEvento.Select(ev => new ListItem { Value = ev.idTipoEvento.ToString(), Text = ev.nombre }).ToList();
            selectTipoEvento.Items.Clear();
            selectTipoEvento.Items.Add(new ListItem { Value = "0", Text = "Seleccione" });
            foreach (ListItem listItem in listListItem)
                selectTipoEvento.Items.Add(listItem);
        }
        else
        {
            buscar();
        }
    }

    void btnNuevo_ServerClick(object sender, EventArgs e)
    {
        Response.Redirect("frmRegistrarEvento.aspx");
    }

    void btnBuscar_ServerClick(object sender, EventArgs e)
    {
    }

    void buscar()
    {
        List<EveEvento> eveEventoList = getFiltroEvento().listarEventos();


        if (eveEventoList.Count > 0)
        {
            foreach (EveEvento item in eveEventoList)
            {

                String productos = String.Empty;
                foreach (EveProductoEvento eveProductoEvento in item.Eve_Producto_evento)
                    productos = productos + eveProductoEvento.Eve_Producto.nombre + "<br>";


                TableRow tr1 = new TableRow();

                TableCell tc1 = new TableCell(); //Cliente
                Label lbl1 = new Label();
                lbl1.Text = item.nombre;
                tc1.Controls.Add(lbl1);

                TableCell tc2 = new TableCell(); //Duracion
                Label lbl2 = new Label();
                lbl2.Text = item.duracion;
                tc2.Controls.Add(lbl2);

                TableCell tc3 = new TableCell(); //Tipo Evento
                Label lbl3 = new Label();
                lbl3.Text = item.Eve_Tipo_evento.nombre;
                tc3.Controls.Add(lbl3);

                TableCell tc4 = new TableCell(); //Productos
                Label lbl4 = new Label();
                lbl4.Text = productos;
                tc4.Controls.Add(lbl4);

                TableCell tc5 = new TableCell(); //Estado
                Label lbl5 = new Label();
                lbl5.Text = item.estado == true ? "Habilitado" : "Deshabilitado";
                tc5.HorizontalAlign = HorizontalAlign.Center;
                tc5.Controls.Add(lbl5);

                //Button Editar
                TableCell tc6 = new TableCell();
                ImageButton imgbuttonEditar = new ImageButton();
                imgbuttonEditar.ImageUrl = "css/img/editar.png";
                imgbuttonEditar.ImageAlign = ImageAlign.AbsMiddle;
                imgbuttonEditar.Attributes.Add("runat", "server");
                imgbuttonEditar.ID = "editar" + item.idEvento;
                imgbuttonEditar.Click += new ImageClickEventHandler(imgbuttonEditar_Click);

                ImageButton imgbuttonEliminar = new ImageButton();
                imgbuttonEliminar.ImageUrl = "css/img/fijo/icono_eliminar.gif";
                imgbuttonEliminar.ImageAlign = ImageAlign.AbsMiddle;
                imgbuttonEliminar.Attributes.Add("runat", "server");
                imgbuttonEliminar.ID = "eliminar" + item.idEvento;
                imgbuttonEliminar.OnClientClick = "Confirm()";
                imgbuttonEliminar.Click += new ImageClickEventHandler(imgbuttonEliminar_Click);

                tc6.Controls.Add(imgbuttonEditar);
                tc6.Controls.Add(imgbuttonEliminar);

                tr1.Cells.Add(tc1);
                tr1.Cells.Add(tc2);
                tr1.Cells.Add(tc3);
                tr1.Cells.Add(tc4);
                tr1.Cells.Add(tc5);
                tr1.Cells.Add(tc6);
                tableReservas.Rows.Add(tr1);
            }
        }
        else
        {
            Page.ClientScript.RegisterStartupScript(GetType(), "msgbox", "alert('No se encontraron registros para la búsqueda realizada');", true);
        }
    }

    void imgbuttonEliminar_Click(object sender, ImageClickEventArgs e)
    {

        string confirmValue = Request.Form["confirm_value"];
        if (confirmValue == "SI")
        {
            ImageButton imageBtn = (ImageButton)sender;
            EveEventoBLL.eliminar(Convert.ToInt32(imageBtn.ID.Replace("eliminar", "")));
            buscar();
        }
    }

    void imgbuttonEditar_Click(object sender, ImageClickEventArgs e)
    {
        ImageButton imageBtn = (ImageButton)sender;
        Response.Redirect("frmModificarEvento.aspx?idEvento=" + imageBtn.ID.Replace("editar",""));
    }


    private EveFiltroEvento getFiltroEvento()
    {
        return new EveFiltroEvento
        {
            Nombre = txtNombre.Value,
            IdTipoEvento = Convert.ToInt32(selectTipoEvento.Value),
        };
    }

}